﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;

public partial class admin_detailCheck : System.Web.UI.Page
{
    rUser ruser;
    DataSet ds;
    DataSet usuEmployee;
    string where1 = "";

    DataSet companies;
    DataSet accounts;
    DataSet products;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null) { Response.Redirect("../Default.aspx"); }
        if (!Page.IsPostBack)
        {
            int id = 0; string temp = "";
            if (Request.QueryString["id"] != null) temp = Request.QueryString["id"].ToString();
            if (Request.QueryString["id"] != null) id = int.Parse(Request.QueryString["id"].ToString());
            if (!Page.IsPostBack)
            {
                id = 0; int dep = 0;
                Utility.setVariablesSession();
                fillCompanies();
                fillAccounts();
                fillclients();
                Punpaid.Visible = false;
                DataSet ds = DB.getBanks();
                llenarcb_bank(ds);
                if (Request.QueryString["id"] != null) id = int.Parse(Request.QueryString["id"].ToString());
                if (Request.QueryString["dep"] != null) dep = int.Parse(Request.QueryString["dep"].ToString());
                lnk_Assign.Visible = false;
                lnk_addmore_unpadid.Visible = false;
                lnk_edit_unpaid.Visible = false;
                lnk_cancel_unpaid.Visible = false;
                lnk_save_unpaid.Visible = false;
                enableInfoControls(false);
                lnk_save.Visible = false;
                lnk_update.Visible = false;
                if (id != 0)
                {// si viene a ver un cheque  en especial


                    RE_GenericBean dschek = DB.GetCheckbyID(id.ToString());
                    cb_bank2.SelectedValue = dschek.strC1;
                    tb_date2.Text = dschek.strC7;
                    tb_date3.Text = dschek.strC8;
                    tb_reference2.Text = dschek.strC2;
                    tb_amount.Text = dschek.strC3;
                    cb_client.SelectedValue = dschek.strC4;
                    cb_company.SelectedValue = dschek.strC5;
                    cb_account.SelectedValue = dschek.strC6;
                    tb_depid.Text = dschek.intC2.ToString();
                    tb_id.Text = id.ToString();

                    if (Session["masivo"] != null)
                    {
                        int perfilID = 0;
                        bt_addor.Visible = false;
                        where1 = (string)Session["wheremsa"];
                        RE_GenericBean permi = Utility.getPermisobyRuser(ruser, 1);
                        RE_GenericBean permi2 = Utility.getPermisobyRuser(ruser, 6);
                        ruser = (rUser)Session["userid"];
                        ds = DB.GetOrdersAcc(permi.int64C1, permi2.int64C1, ruser.getAlias(), where1, "", "", 1, perfilID);
                        //ds = DB.GetOrdersAcc(31, 31, "LB", where1, "", "", 1, perfilID);

                        if (ds != null && ds.Tables[0].Rows.Count > 0)
                        {
                            Panel2.Visible = true;
                            lnk_Assign.Enabled = true;
                            Session["masivo"] = null;
                            gv_multiple.DataSource = ds;
                            gv_multiple.DataBind();
                            lnk_Assign.Visible = true;
                            lnk_addmore_unpadid.Visible = false;
                        }
                        else {
                            bt_addor.Visible = false;
                            Panel1.Visible = true;
                            lnk_save.Visible = false;
                            lnk_update.Visible = false;
                            lnk_addmore_unpadid.Visible = false;
                            lb_error.Text = "No orders found";
                            lb_error.Visible = true;
                        
                        
                        }
                        enableInfoControls(false);

                    }

                }
                else
                {

                    tb_depid.Text = dep.ToString();
                    int nuevo = 0;

                    if (Session["newCheck"] != null)
                    {
                        nuevo = (int)Session["newCheck"];
                        if (nuevo == 1)
                        {
                            Session["newCheck"] = null;
                            Pdetail.Visible = true;
                            bt_addor.Visible = false;
                            bt_cancel.Visible = true;
                            enableInfoControls(true);
                            tb_depid.Text = dep.ToString();
                            Edit.Visible = false;
                            lnk_save.Visible = true;
                            lnk_Assign.Visible = false;
                            lnk_update.Visible = false;
                        }
                        else
                        {
                            bt_addor.Visible = false;
                            lnk_Assign.Visible = false;
                            bt_addor.Visible = false;

                        }

                    }
                    else
                    {
                        Session["newCheck"] = null;
                        Pdetail.Visible = true;
                        bt_addor.Visible = false;
                        bt_cancel.Visible = true;
                        enableInfoControls(true);
                        tb_depid.Text = dep.ToString();
                        Edit.Visible = false;
                        lnk_save.Visible = true;
                        lnk_update.Visible = false;
                        lnk_Assign.Visible = false;

                    }


                }
            }

             fillOrders(id.ToString());
            int comprobar = comprobarsum();

        }
    }

    private  void fillOrders(string id){
    
         DataSet dsorders = DB.GetOrdersbyCheck(id);

            if (dsorders != null)
            {
                gv_order.DataSource = dsorders;
                gv_order.DataBind();
                int cont = dsorders.Tables[0].Rows.Count - 1;
                if (cont >= 0)
                {

                    string tst = String.Format("{0:0.00}", double.Parse(dsorders.Tables[0].Rows[cont][14].ToString()));
                    tb_sum.Text = tst;
                }
                else
                {
                    tb_sum.Text = "0";
                }
            }
    
    }

private int comprobarsum()
{
    int result = 0;
    // en cualquier caso que pinte color sumatoria
    if (!tb_sum.Text.Equals("") && (!tb_amount.Text.Equals("")))
    {
        double temp = double.Parse(tb_sum.Text);
        double ammoun = double.Parse(tb_amount.Text);
        if (temp < ammoun)
        {
            tb_sum.ForeColor = System.Drawing.Color.OrangeRed;
            //tb_total.BackColor = System.Drawing.Color.OrangeRed;
            result = 0;
            lb_msg.Text = "The sum of the orders is less than the amount of the check";
        }
        else if (temp > ammoun)
        {
            tb_sum.ForeColor = System.Drawing.Color.Red;
            //tb_total.BackColor = System.Drawing.Color.Blue;
            result = 2;
            lb_msg.Text = "The sum of orders is greater than the amount of the check"; 
        }
        else
        {
            tb_sum.ForeColor = System.Drawing.Color.DarkGreen;
            //tb_total.BackColor = System.Drawing.Color.Red;
            result = 1;
            lb_msg.Text = "Exit?"; 
        }
        

}
    return result;
    }
    

    private void fillCompanies()
    {
        try
        {
            DataSet dsc = (DataSet)(Session["companies"]);
            if (dsc != null)
            {
                ListItem listitem0 = new ListItem();
                listitem0.Text = "All"; listitem0.Value = "All";
                cb_company.Items.Add(listitem0);

                for (int ii = 0; ii <= (dsc.Tables[0].Rows.Count - 1); ii++)
                {
                    ListItem listitem = new ListItem();
                    listitem.Text = dsc.Tables[0].Rows[ii][1].ToString();
                    listitem.Value = dsc.Tables[0].Rows[ii][0].ToString();
                    cb_company.Items.Add(listitem);

                }

            }

        }
        catch (Exception ec)
        {
            Utility.writeError("WS getCompanies no devolvio companias " + ec.Message);
        }
    }

    private void fillAccounts()
    {
        try
        {
            DataSet dsc = (DataSet)(Session["accounts"]);
            if (dsc != null)
            {
                ListItem listitem0 = new ListItem();
                listitem0.Text = "All"; listitem0.Value = "All";
                cb_account.Items.Add(listitem0);

                for (int ii = 0; ii <= (dsc.Tables[0].Rows.Count - 1); ii++)
                {
                    ListItem listitem = new ListItem();
                    listitem.Text = dsc.Tables[0].Rows[ii][1].ToString();
                    listitem.Value = dsc.Tables[0].Rows[ii][0].ToString();
                    cb_account.Items.Add(listitem);
                }

            }

        }
        catch (Exception ec)
        {
            Utility.writeError("WS getCompanies no devolvio companias " + ec.Message);
        }
    }

    private void fillclients()
    {
        try
        {
            string rxml = null;
            com.bpophotos.www.Service service = new com.bpophotos.www.Service();
            rxml = service.UsuariosPorPerfil(1);

            if (!rxml.Equals("") || rxml != null)
            {
                DataSet dsclient = Utility.getUsuariosporPerfil(rxml);
                ListItem listitem0 = new ListItem();
                listitem0.Text = "All"; listitem0.Value = "All";
                cb_client.Items.Add(listitem0);


                for (int ii = 0; ii <= (dsclient.Tables[0].Rows.Count - 1); ii++)
                {
                    ListItem listitem = new ListItem();
                    listitem.Text = dsclient.Tables[0].Rows[ii][2].ToString() + " " + dsclient.Tables[0].Rows[ii][1].ToString();
                    listitem.Value = dsclient.Tables[0].Rows[ii][0].ToString();
                    cb_client.Items.Add(listitem);

                    /*
                    if (client.Equals(dsclient.Tables[0].Rows[ii][0].ToString()))
                    {
                        cb_client.SelectedValue = dsclient.Tables[0].Rows[ii][0].ToString();
                       
                    }*/
                }
            }

        }
        catch (Exception ee)
        {
            Utility.writeError(ee.Message);
        }
    }
    private void llenarcb_bank(DataSet ds)
    {
        if (ds != null)
        {
            ListItem li = new ListItem();
            li.Text = "Select One";
            li.Value = "0";
            cb_bank2.Items.Add(li);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (ds.Tables[0].Rows[i][2].ToString() == "2")
                {

                    li = new ListItem();
                    li.Value = ds.Tables[0].Rows[i][0].ToString();
                    li.Text = ds.Tables[0].Rows[i][1].ToString();
                    cb_bank2.Items.Add(li);
                }
            }
        }
    }
    protected void Button8_Click(object sender, EventArgs e)
    {
        bt_addor.Visible = false;
        Panel1.Visible = true;
        lnk_save.Visible = false;
        lnk_update.Visible = false;
        lnk_addmore_unpadid.Visible = false;

    }
    protected void lnk_Assign_Click(object sender, EventArgs e)
    {
        Utility.setVariablesSession();
        lb_error.Visible = false; lb_mensaje.Visible = false;
        string ordersgroup = ""; int p = 0; int pos = 0;
        ordersgroup = " Where order_id in (";
        string[] orders = new string[100];
        ArrayList array = new ArrayList();
        int cont = 0;
        foreach (GridViewRow ROW in gv_multiple.Rows)
        {

            string ordernum = ""; string fee = "0";

            CheckBox chk = (CheckBox)ROW.FindControl("CHBX1");
            if (chk.Checked)
            {
                Label label = (Label)ROW.FindControl("lb_order");
                if (label != null) { ordernum = label.Text;
                cont++;
                if (cont != 1)
                {
                    ordersgroup = ordersgroup + " , " + ordernum ;
                }
                else
                {
                    ordersgroup = ordersgroup + " " + ordernum + " ";
                }
                }
                TextBox txt = (TextBox)ROW.FindControl("tbx_fee");
                if (!txt.Text.Equals(""))
                {
                    fee = txt.Text;
                }

                RE_GenericBean rg = new RE_GenericBean();
                rg.strC1 = ordernum;
                rg.strC2 = fee;
                rg.strC3 = tb_id.Text;
                array.Add(rg);

            }
           
        }
        ordersgroup = ordersgroup+ " ) ";
        ruser = (rUser)Session["userid"];
        RE_GenericBean resop = DB.setPaid(array, ruser.getUsuarioId());
       // RE_GenericBean resop = DB.setPaid(array, "LUCIANAB");
        lnk_cancel_unpaid.Visible = true;
        lnk_edit_unpaid.Visible = true;
        Edit.Visible = false;
        //lnk_addmore_unpadid.Visible = true;
        bt_addor.Visible = true;
        lnk_edit_unpaid.Visible = true;

        lnk_save.Visible = false;
        lnk_update.Visible = false;
        lnk_save_unpaid.Visible = false;
        lnk_Assign.Visible = false;


        bt_addor.Visible = false;


       
      
       
        if (resop.intC1 == 1)
        {
            lb_mensaje.Text = "Paid Set";
            lb_mensaje.Visible = true;
            Panel2.Visible = false;
            
           
        }
        else
        {
            lb_error.Text = "Paid not Set:  " + resop.strC1;
            lb_error.Visible = true;
            lnk_cancel_unpaid.Visible = false;
            lnk_edit_unpaid.Visible = false;
            Edit.Visible = true;
            lnk_addmore_unpadid.Visible = true;
            lnk_addmore_unpadid.Enabled = true;
            bt_addor.Visible = false;
            lnk_edit_unpaid.Visible = false;


        }




        where1 = ordersgroup;
        //int perfilID = int.Parse(ruser.getPerfilId());
        int perfilID = 0;
        RE_GenericBean permi = Utility.getPermisobyRuser(ruser, 1);
        RE_GenericBean permi2 = Utility.getPermisobyRuser(ruser, 6);
       
       // ds = DB.GetOrdersAcc(permi.int64C1, permi2.int64C1, ruser.getAlias(), where1, "", "", 0, 0);
        //ds = DB.GetOrdersAcc(31, 31, "LB", where1, "", "", 0, 0);
       // Punpaid.Visible = true;
        //gv_unpaid.DataSource = ds;
        //gv_unpaid.DataBind();
        
        //Panel2.Visible = false;

        fillOrders(tb_id.Text);
       
      
      
    
    }
    protected void lnk_search_Click(object sender, EventArgs e)
    {    
       
        string ordersgroup = tb_orders.Text.Trim();

        ordersgroup = ordersgroup.Replace(",", "','");
        ordersgroup = "'" + ordersgroup + "'";
        ordersgroup = "and ordernum in (" + ordersgroup + ")";

            //ordersgroup = ordersgroup + " and ([ORDER].status_id = 3 or [ORDER].status_id = 4 or [ORDER].status_id = 5)";

       
        int id=0;
         if (Request.QueryString["id"] != null) id = int.Parse(Request.QueryString["id"].ToString());
         Panel2.Visible = true;
        Session["masivo"] = ordersgroup;
        Session["wheremsa"] = ordersgroup;
        if (!tb_id.Text.Equals("")) { id = int.Parse(tb_id.Text); }
        string dep="";
        if (!tb_depid.Text.Equals("")) { dep = tb_depid.Text; }
        
        Response.Redirect("detailCheck.aspx?id="+id+"&dep="+dep);
    }
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow ROW in gv_multiple.Rows)
        {


            CheckBox chk = (CheckBox)ROW.FindControl("CHBX1");
            if (CheckBox1.Checked)
            {
                chk.Checked = true;
            }
            else
            {
                chk.Checked = false;
            }

        }
    }
    protected void gv_multiple_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            TextBox isPaid = (TextBox)e.Row.FindControl("isPaid");
            string paid = isPaid.Text;
            if (paid == "false")
            {
                e.Row.FindControl("image").Visible = false;
            }
            e.Row.FindControl("isPaid").Visible = false;
        }
           

    }
    protected void bt_cancel_Click(object sender, EventArgs e)
    {

        int comprobar = comprobarsum();
        if (comprobar == 0) { PMsg_ModalPopupExtender.Show(); }
        if (comprobar == 1)
        {
            int dep = 0;
            if (!tb_depid.Text.Equals("")) dep = int.Parse(tb_depid.Text);
            if (dep != 0) { Response.Redirect("detailDeposit.aspx?id=" + dep); }
            else
            {
                Response.Redirect("Accounting.aspx");
            }
            if (comprobar == 2) { PMsg_ModalPopupExtender.Show(); }



        }
    }
    protected void Edit_Click(object sender, EventArgs e)
    {
        enableInfoControls(true);
        Edit.Visible = false;
        lnk_update.Visible = true;
    }

    private void enableInfoControls(bool p)
    {
        cb_bank2.Enabled = p;
        cb_client.Enabled = p;
        cb_company.Enabled = p;
        cb_account.Enabled = p;
        tb_date2.Enabled = p;
        tb_date3.Enabled = p;
        tb_reference2.Enabled = p;
        tb_amount.Enabled = p;
    }


    protected int  save() {
        int result = 0;
        if (tb_depid.Text != "0" && tb_depid.Text != "" && cb_bank2.SelectedIndex!=0 && !tb_reference2.Text.Equals("") && !tb_date2.Text.Equals("") && !tb_amount.Text.Equals("") && cb_client.SelectedIndex!=0 && cb_company.SelectedIndex!=0 && cb_account.SelectedIndex!=0 && !tb_date3.Text.Equals(""))
        {
            int dep = int.Parse(tb_depid.Text);
            RE_GenericBean rg = new RE_GenericBean();
            int id = 0;
            if (!tb_id.Text.Equals("")) id = int.Parse(tb_id.Text);
            rg.intC1 = id;
            rg.intC2 = dep;
            rg.intC3 = int.Parse(cb_bank2.SelectedValue);
            rg.strC5 = cb_client.SelectedValue;
            rg.intC5 = int.Parse(cb_company.SelectedValue);
            rg.intC6 = int.Parse(cb_account.SelectedValue);
            rg.strC1 = tb_reference2.Text;
            rg.strC2 = tb_date2.Text;
            rg.strC3 = tb_date3.Text;
            rg.strC4 = tb_amount.Text;

            int resp = DB.addCheck(dep, rg);
            if (resp != 0)
            {
                bt_addor.Visible = true; lnk_Assign.Visible = false; lnk_update.Visible = false;
                tb_id.Text = resp.ToString();
                tb_depid.Text = dep.ToString();
                //Response.Redirect("detailCheck.aspx?id=" + resp + "&dep=" + dep);
                Edit.Visible = true;
                enableInfoControls(false);
                lnk_save.Visible = false;
                bt_addor.Visible = true;
                result = 1;
            }
            else
            {
                string texto = "<script> alert('Check no add for this deposit');</script>";
                if (resp == -1) { texto = "<script> alert('Check Exist!!');</script>"; }
                Response.Write(texto);
                Edit.Visible = false;
                lnk_save.Visible = true;
                bt_addor.Visible = false;
                result = 0;
            }

        }
        else {
            Response.Write("<script> alert('Check no add , missing one or more fields');</script>");
            Edit.Visible = false;
            lnk_save.Visible = true;
            bt_addor.Visible = false;
            result = 0;
        
        }

        return result;
    }

    protected void lnk_save_Click(object sender, EventArgs e)
    {
        int res= save();
        if (res==1){
        Panel1.Visible = true;
        Edit.Visible = true;
        lnk_update.Visible = false;
        lnk_Assign.Visible = false;
        lnk_save.Visible = false;
        }

    }
    protected void gv_order_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
           
          /*  e.Row.Cells[1].Text = "Order #";
            e.Row.Cells[2].Text = "Company";
            e.Row.Cells[3].Text = "Account";
            e.Row.Cells[4].Text = "Type";
            e.Row.Cells[5].Text = "St #";
            e.Row.Cells[6].Text = "Address";
            e.Row.Cells[7].Text = "Type";
            e.Row.Cells[8].Text = "Fee";
            e.Row.Cells[9].Text = "Paid";
            e.Row.Cells[10].Text = "Paid Date";
            e.Row.Cells[11].Text = "Status";*/
           // e.Row.Cells[8].Width = 200;
           // e.Row.Cells[10].Style.Add("display", "none");
           // e.Row.Cells[12].Style.Add("display", "none");
           // e.Row.Cells[15].Style.Add("display", "none");
            for (int i = 0; i < e.Row.Cells.Count; i++)
            {
                e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Left;
            }
        }
          if (e.Row.RowType == DataControlRowType.DataRow)
        {
              // e.Row.Cells[8].Width = 200;
              // e.Row.Cells[10].Style.Add("display", "none");
              // e.Row.Cells[12].Style.Add("display", "none");
              // e.Row.Cells[15].Style.Add("display", "none");
               /*string paid = e.Row.Cells[13].Text;
               if (paid == "YES")
               {
                   e.Row.Cells[13].Text = "<img src='../../img/checkdone.png'>";
               }*/

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                CheckBox chk = (CheckBox)e.Row.FindControl("CHBX3");
                TextBox isPaid = (TextBox)e.Row.FindControl("isPaid");
                string paid = isPaid.Text;
                if (paid == "false")
                {
                   
                    chk.Checked=false;
                }
                else
                {
                    chk.Checked = true;
                }
                e.Row.FindControl("isPaid").Visible = false;
            }
           
        }
    }
    protected void lnk_addmore_unpadid_Click(object sender, EventArgs e)
    {
        addmore();

    }
    protected void addmore() {
        Panel1.Visible = true;
        Punpaid.Visible = false;
        lnk_save.Visible = false;
        Edit.Visible = false;
        bt_addor.Visible = false;
        bt_cancel.Visible = true;
        DataSet dsorders = DB.GetOrdersbyCheck(tb_id.Text);

        if (dsorders != null)
        {
            gv_order.DataSource = dsorders;
            gv_order.DataBind();
            int cont = dsorders.Tables[0].Rows.Count - 1;
            if (cont >= 0)
            {
                string tst = String.Format("{0:0.00}", double.Parse(dsorders.Tables[0].Rows[cont][14].ToString()));
                tb_sum.Text = tst;
            }
            else
            {
                tb_sum.Text = "0";
            }
        }
    }
    protected void gv_unpaid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[2].Text = "Test";
        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            CheckBox chk = (CheckBox)e.Row.FindControl("CHBX2");
            TextBox isPaid = (TextBox)e.Row.FindControl("isPaidUn");
            string paid = isPaid.Text;
            if (paid == "false")
            {
                e.Row.FindControl("imageUn").Visible = false;
            }
            else {
                chk.Checked = true;
            }
            e.Row.FindControl("isPaidUn").Visible = false;
        }
           
    }
    protected void lnk_edit_unpaid_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow ROW in gv_unpaid.Rows)
        {


            CheckBox chk = (CheckBox)ROW.FindControl("CHBX2");
            chk.Enabled = true;
            TextBox tb = (TextBox)ROW.FindControl("tbx_feeUn");
            tb.Enabled = true;
        }
        lnk_save_unpaid.Visible = true;
        lnk_edit_unpaid.Visible = false;
        lnk_addmore_unpadid.Visible = false;
        bt_addor.Visible = false;
    }
    protected void lnk_cancel_unpaid_Click(object sender, EventArgs e)
    {
        Punpaid.Visible = false;
        Edit.Visible = true;
        bt_addor.Visible = true;
        bt_cancel.Visible = true;
        lnk_save.Visible = false;
        lnk_update.Visible = false;
        lnk_cancel_unpaid.Visible = false;
        lnk_addmore_unpadid.Visible = false;
        lnk_edit_unpaid.Visible = false;
        
        DataSet dsorders = DB.GetOrdersbyTrans(tb_id.Text);

       

        if (dsorders != null)
        {
            gv_order.DataSource = dsorders;
            gv_order.DataBind();
            int cont = dsorders.Tables[0].Rows.Count - 1;
            if (cont >= 0)
            {
                string tst = String.Format("{0:0.00}", double.Parse(dsorders.Tables[0].Rows[cont][14].ToString()));
                tb_sum.Text = tst;
            }
            else
            {
                tb_sum.Text = "0";
            }
        }
      
    }
    protected void lnk_save_unpaid_Click(object sender, EventArgs e)
    {
        Utility.setVariablesSession();
        Label5.Visible = false; Label6.Visible = false;
        Edit.Visible = false;
        string ordersgroup = ""; int p = 0; int pos = 0;
        ordersgroup = " Where order_id in (";
        string[] orders = new string[100];
        ArrayList arrayun = new ArrayList();
        ArrayList arraypaid = new ArrayList();
        int cont = 0;
        foreach (GridViewRow ROW in gv_unpaid.Rows)
        {

            string ordernum = ""; string fee = "0";

           
                Label label = (Label)ROW.FindControl("lb_orderUn");
                if (label != null)
                {
                    ordernum = label.Text;
                    cont++;
                    if (cont != 1)
                    {
                        ordersgroup = ordersgroup + " , " + ordernum;
                    }
                    else
                    {
                        ordersgroup = ordersgroup + " " + ordernum + " ";
                    }
                }

            
            TextBox txt = (TextBox)ROW.FindControl("tbx_feeUn");
            if (!txt.Text.Equals(""))
            {
                fee = txt.Text;
            }

            CheckBox chk = (CheckBox)ROW.FindControl("CHBX2");
            if (!chk.Checked)
            {
                RE_GenericBean rg = new RE_GenericBean();
                rg.strC1 = ordernum;
                rg.strC2 = fee;
                rg.strC3 = tb_id.Text;
                arrayun.Add(rg);
            }
            else {
                RE_GenericBean rg = new RE_GenericBean();
                rg.strC1 = ordernum;
                rg.strC2 = fee;
                rg.strC3 = tb_id.Text;
                arraypaid.Add(rg);
                
            }
        }
        ordersgroup = ordersgroup + " ) ";
        ruser = (rUser)Session["userid"];
        RE_GenericBean resop = DB.setUnPaid(arrayun, ruser.getUsuarioId());
        RE_GenericBean respaid = DB.setPaid(arraypaid, ruser.getUsuarioId());
       // RE_GenericBean resop = DB.setUnPaid(array, "LUCIANAB");
        if (resop.intC1 == 1)
        {
            lb_mensaje.Text = "Unpayment applied";
            lb_mensaje.Visible = true;
        }
        else
        {
            lb_error.Text = "Unpayment was not applied " + resop.strC1;
            lb_error.Visible = true;
        }


        if (respaid.intC1 == 1)
        {
            lb_mensaje.Text = "Payment applied";
            lb_mensaje.Visible = true;
        }
        else
        {
            lb_error.Text = "Payment was not applied " + respaid.strC1;
            lb_error.Visible = true;
        }
        ruser = (rUser)Session["userid"];
        where1 = ordersgroup;
        //int perfilID = int.Parse(ruser.getPerfilId());
        int perfilID = 0;
        RE_GenericBean permi = Utility.getPermisobyRuser(ruser, 1);
        RE_GenericBean permi2 = Utility.getPermisobyRuser(ruser, 6);

        ds = DB.GetOrdersAcc(permi.int64C1, permi2.int64C1, ruser.getAlias(), where1, "", "", 0, 0);
        //ds = DB.GetOrdersAcc(31, 31, "LB", where1, "", "", 0, 0);
        Punpaid.Visible = true;
        gv_unpaid.DataSource = ds;
        gv_unpaid.DataBind();

        Panel2.Visible = false;
        lnk_save_unpaid.Visible = false;
        lnk_save.Visible = false;
        bt_cancel.Visible = false;
        lnk_edit_unpaid.Visible = true;
        lnk_addmore_unpadid.Visible = true;
        lnk_addmore_unpadid.Enabled = true;
        bt_addor.Visible = false;

        DataSet dsorders = DB.GetOrdersbyCheck(tb_id.Text);

        if (dsorders != null)
        {
            gv_order.DataSource = dsorders;
            gv_order.DataBind();
            int cont2 = dsorders.Tables[0].Rows.Count - 1;
            if (cont2 >= 0)
            {
                
                string tst = String.Format("{0:0.00}", double.Parse(dsorders.Tables[0].Rows[cont2][14].ToString()));
                tb_sum.Text = tst;
            }
            else
            {
                tb_sum.Text = "0";
            }
        }
         
        
    
    }
    protected void lnk_msg_ok0_Click(object sender, EventArgs e)
    {
        int dep = 0;
        if (!tb_depid.Text.Equals("")) dep = int.Parse(tb_depid.Text);
        if (dep != 0) { Response.Redirect("detailDeposit.aspx?id="+dep); } else { Response.Redirect("Accounting.aspx"); }

        
    }

    protected void imaButton_Click(object sender, ImageClickEventArgs e)
    {

    }
    protected void lnk_update_Click(object sender, EventArgs e)
    {
        save();
        Edit.Visible = true;
        lnk_update.Visible = false;
        lnk_Assign.Visible = false;
        lnk_save.Visible = false;
        lnk_addmore_unpadid.Visible = false;

    }
   
    protected void gvd_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        lb_error.Visible = false; lb_mensaje.Visible = false;
        if (e.CommandName == "Edit")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            
            int i=0;
            Control c= (Control)sender;
            
           

            foreach (GridViewRow ROW in gv_order.Rows)
            {
                if (i == index)
                {
                    int con= ROW.Controls.Count;
                    TextBox tb = (TextBox)ROW.FindControl("tbx_fee");
                    CheckBox chk = (CheckBox)ROW.FindControl("CHBX3");
                    Image img = (Image)ROW.FindControl("image");
                    Button bt = (Button)ROW.FindControl("bt_savedetail");
                    bt.Visible = true;
                    img.Visible = false;
                    chk.Visible = true;
                    chk.Enabled = true;
                    tb.Enabled = true;
                   

                }
               
                    i++;
            }
            
        }
        if (e.CommandName == "Save")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            int i = 0;
            
            ArrayList array = new ArrayList();
            ArrayList arrayno = new ArrayList();
            foreach (GridViewRow ROW in gv_order.Rows)
            {
                if (i == index)
                {
                    int con = ROW.Controls.Count;
                    TextBox tb = (TextBox)ROW.FindControl("tbx_fee");
                    CheckBox chk = (CheckBox)ROW.FindControl("CHBX3");
                    string ordernum = "";
                    string fee = "";
                    Label label = (Label)ROW.FindControl("lb_order");
                    if (label != null)
                    {
                        ordernum = label.Text;

                    }

                    if (!tb.Text.Equals(""))
                    {
                        fee = tb.Text;
                    }
                    if (chk.Checked)
                    {
                      

                        RE_GenericBean rg = new RE_GenericBean();
                        rg.strC1 = ordernum;
                        rg.strC2 = fee;
                        rg.strC3 = tb_id.Text;
                        array.Add(rg);
                        chk.Enabled = false;
                        chk.Visible = false;
                        tb.Enabled = false;
                    }
                    else {
                       
                        RE_GenericBean rg = new RE_GenericBean();
                        rg.strC1 = ordernum;
                        rg.strC2 = fee;
                        rg.strC3 = tb_id.Text;
                        arrayno.Add(rg);
                        chk.Enabled = false;
                        chk.Visible = false;
                        tb.Enabled = false;
                    
                    }

                    

                }
                
                i++;
            }

            RE_GenericBean resop=  new RE_GenericBean();
           
            if (array.Count>0) { resop= DB.setPaid(array, "");}
            if (arrayno.Count>0) { resop= DB.setUnPaid(arrayno, "");}
            if (resop.intC1 == 1)
            {
                lb_mensaje.Text = "Change applied";
                lb_mensaje.Visible = true;
                fillOrders(tb_id.Text);


            }
            else
            {
                lb_error.Text = "Change not applied:  " + resop.strC1;
                lb_error.Visible = true;
                fillOrders(tb_id.Text);

            }   
           
        }
    }

    protected void bt_Save_Click(object sender, EventArgs e)
    {
        Button but = (Button)sender;
        but.Visible = false;
    }
    protected void gv_order_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }

    protected void savedetail(object sender, EventArgs e)
    {
        int index = 0;
        ImageButton bt = (ImageButton)sender;
        GridViewRow objFila = (GridViewRow)bt.Parent.Parent;
        ImageButton btedit = (ImageButton)objFila.FindControl("bt_editdetail");
        btedit.Visible = false;

        index = objFila.RowIndex;
       
        
            int i = 0;
            
            ArrayList array = new ArrayList();
            ArrayList arrayno = new ArrayList();
            foreach (GridViewRow ROW in gv_order.Rows)
            {
                if (i == index)
                {
                    int con = ROW.Controls.Count;
                    TextBox tb = (TextBox)ROW.FindControl("tbx_fee");
                    TextBox tb2 = (TextBox)ROW.FindControl("tbx_fee2");
                    ImageButton bt2 = (ImageButton)ROW.FindControl("bt_savedetail");
                    bt2.Visible = false;

                    CheckBox chk = (CheckBox)ROW.FindControl("CHBX3");
                    string ordernum = "";
                    string fee = "";
                    string fee2 = "";
                    Label label = (Label)ROW.FindControl("lb_order");
                    if (label != null)
                    {
                        ordernum = label.Text;

                    }

                    if (!tb.Text.Equals(""))
                    {
                        fee = tb.Text;
                    }
                    if (!tb2.Text.Equals(""))
                    {
                        fee2 = tb2.Text;
                    }
                    if (chk.Checked)
                    {
                      

                        RE_GenericBean rg = new RE_GenericBean();
                        rg.strC1 = ordernum;
                        rg.strC2 = fee;
                        rg.strC3 = tb_id.Text;
                        rg.strC4 = fee2;
                        array.Add(rg);
                        chk.Enabled = false;
                        chk.Visible = false;
                        tb.Enabled = false;
                    }
                    else {
                       
                        RE_GenericBean rg = new RE_GenericBean();
                        rg.strC1 = ordernum;
                        rg.strC2 = fee;
                        rg.strC3 = tb_id.Text;
                        arrayno.Add(rg);
                        chk.Enabled = false;
                        chk.Visible = false;
                        tb.Enabled = false;
                    
                    }

                    

                }
                
                i++;
            }

            RE_GenericBean resop=  new RE_GenericBean();
           
            if (array.Count>0) { resop= DB.setPaid(array, "");}
            if (arrayno.Count>0) { resop= DB.setUnPaid(arrayno, "");}
            if (resop.intC1 == 1)
            {
                lb_mensaje.Text = "Change applied";
                lb_mensaje.Visible = true;
                fillOrders(tb_id.Text);


            }
            else
            {
                lb_error.Text = "Change not applied:  " + resop.strC1;
                lb_error.Visible = true;
                fillOrders(tb_id.Text);

            }   
           
        }

    protected void editdetail(object sender, EventArgs e)
    {
        int index = 0;

        ImageButton bt = (ImageButton)sender;
        bt.Visible = false;
        GridViewRow objFila = (GridViewRow)bt.Parent.Parent;
        ImageButton btedit = (ImageButton)objFila.FindControl("bt_savedetail");
        btedit.Visible = true;

        index = objFila.RowIndex;
        int i = 0;
   



        foreach (GridViewRow ROW in gv_order.Rows)
        {
            if (i == index)
            {
                int con = ROW.Controls.Count;
                TextBox tb = (TextBox)ROW.FindControl("tbx_fee");
                CheckBox chk = (CheckBox)ROW.FindControl("CHBX3");
                Image img = (Image)ROW.FindControl("image");
                
                img.Visible = false;
                chk.Visible = true;
                chk.Enabled = true;
                tb.Enabled = true;


            }

            i++;
        }
    }
    
}